<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body style="height: 1000px;">
    <button style="position: fixed;right: 100px;bottom: 100px;display: none;">↑</button>
    <script>
        let btn=document.querySelector("button")
        function fun(fn){
            let time=true;
            function exe(){
                if(time){
                    time=false
                    setTimeout(() => {
                        fn()
                        time=true
                    }, 500);
                }
            }
            return exe
        }
        onscroll=fun(()=>{
            if(document.documentElement.scrollTop>0){
                btn.style.display="block";
                console.log(1)
            }else{
                btn.style.display="none"
                console.log(2)
            }
        })
        btn.addEventListener("click",function(){
            document.documentElement.scrollTop=0
        })
    </script>
</body>
</html>